//! Copyright 2011 Xicut Technology, Inc.
//! All rights reserved
//!
#ifndef __BoardHardware_h__
#define __BoardHardware_h__

// #define BOARD_V1
#define BOARD_V2

#define MACHINE1
//#define MACHINE2
//#define MACHINE3


#if defined ( BOARD_V1 )

#elif defined ( BOARD_V2 )
//--------------------------------------------------------------
// define LED
#define LED_PERIPH    RCC_APB2Periph_GPIOA
#define LED_PORT      GPIOA
#define LED_PIN       GPIO_Pin_2
#if defined (__FW_BUILD__)
#define LED_OFF       GPIOA->BRR = (1<<2)
#define LED_ON        GPIOA->BSRR  = (1<<2)
#define LED_INV       if ((GPIOA->IDR & (1<<2))!=0) LED_OFF; else LED_ON
#else
#define LED_OFF       0
#define LED_ON        0
#define LED_INV       0
#endif

// define GPIO0
#define GPIO0_PERIPH    RCC_APB2Periph_GPIOB
#define GPIO0_PORT      GPIOB
#define GPIO0_PIN       GPIO_Pin_5
#if defined (__FW_BUILD__)
#define GPIO0_OFF       GPIOB->BRR = (1<<5)
#define GPIO0_ON        GPIOB->BSRR  = (1<<5)
#define GPIO0_INV       if ((GPIOB->IDR & (1<<5))!=0) GPIO0_OFF; else GPIO0_ON
#else
#define GPIO0_OFF       0
#define GPIO0_ON        1
#define GPIO0_INV       0
#endif

// define GPIO1
#define GPIO1_PERIPH    RCC_APB2Periph_GPIOB
#define GPIO1_PORT      GPIOB
#define GPIO1_PIN       GPIO_Pin_6
#if defined (__FW_BUILD__)
#define GPIO1_OFF       GPIOB->BRR = (1<<6)
#define GPIO1_ON        GPIOB->BSRR  = (1<<6)
#define GPIO1_INV       if ((GPIOB->IDR & (1<<6))!=0) GPIO1_OFF; else GPIO1_ON
#else
#define GPIO1_OFF       0
#define GPIO1_ON        1
#define GPIO1_INV       0
#endif

// define GPIO2
#define GPIO2_PERIPH    RCC_APB2Periph_GPIOB
#define GPIO2_PORT      GPIOB
#define GPIO2_PIN       GPIO_Pin_7
#if defined (__FW_BUILD__)
#define GPIO2_OFF       GPIOB->BRR = (1<<7)
#define GPIO2_ON        GPIOB->BSRR  = (1<<7)
#define GPIO2_INV       if ((GPIOB->IDR & (1<<7))!=0) GPIO2_OFF; else GPIO2_ON
#else
#define GPIO2_OFF       0
#define GPIO2_ON        1
#define GPIO2_INV       0
#endif

// define GPIO3
#define GPIO3_PERIPH    RCC_APB2Periph_GPIOB
#define GPIO3_PORT      GPIOB
#define GPIO3_PIN       GPIO_Pin_8
#if defined (__FW_BUILD__)
#define GPIO3_OFF       GPIOB->BRR = (1<<8)
#define GPIO3_ON        GPIOB->BSRR  = (1<<8)
#define GPIO3_INV       if ((GPIOB->IDR & (1<<8))!=0) GPIO3_OFF; else GPIO3_ON
#else
#define GPIO3_OFF       0
#define GPIO3_ON        1
#define GPIO3_INV       0
#endif

// define GPIO4
#define GPIO4_PERIPH    RCC_APB2Periph_GPIOB
#define GPIO4_PORT      GPIOB
#define GPIO4_PIN       GPIO_Pin_9
#if defined (__FW_BUILD__)
#define GPIO4_OFF       GPIOB->BRR = (1<<9)
#define GPIO4_ON        GPIOB->BSRR  = (1<<9)
#define GPIO4_INV       if ((GPIOB->IDR & (1<<9))!=0) GPIO4_OFF; else GPIO4_ON
#else
#define GPIO4_OFF       0
#define GPIO4_ON        1
#define GPIO4_INV       0
#endif

//--------------------------------------------------------------
// khai bao chan SPI1 ket noi voi ENC28J60
#define ENC28J60_SPI                       SPI1
#define ENC28J60_SPI_CLK                   RCC_APB2Periph_SPI1
        
#define ENC28J60_SPI_SCK_PIN               GPIO_Pin_5                   /* PA.5 */
#define ENC28J60_SPI_SCK_GPIO_PORT         GPIOA                        /* GPIOA */
#define ENC28J60_SPI_SCK_GPIO_CLK          RCC_APB2Periph_GPIOA
#define ENC28J60_SPI_SCK_LOW               ENC28J60_SPI_SCK_GPIO_PORT->BRR = (1<<5)
#define ENC28J60_SPI_SCK_HIGH              ENC28J60_SPI_SCK_GPIO_PORT->BSRR = (1<<5)
        
#define ENC28J60_SPI_MISO_PIN              GPIO_Pin_6                   /* PA.6 */
#define ENC28J60_SPI_MISO_GPIO_PORT        GPIOA                        /* GPIOA */
#define ENC28J60_SPI_MISO_GPIO_CLK         RCC_APB2Periph_GPIOA
#define ENC28J60_SPI_MISO_LOW              ENC28J60_SPI_MISO_GPIO_PORT->BRR = (1<<6)
#define ENC28J60_SPI_MISO_HIGH             ENC28J60_SPI_MISO_GPIO_PORT->BSRR = (1<<6)
        
#define ENC28J60_SPI_MOSI_PIN              GPIO_Pin_7                   /* PA.7 */
#define ENC28J60_SPI_MOSI_GPIO_PORT        GPIOA                        /* GPIOA */
#define ENC28J60_SPI_MOSI_GPIO_CLK         RCC_APB2Periph_GPIOA
#define ENC28J60_SPI_MOSI_LOW              ENC28J60_SPI_MOSI_GPIO_PORT->BRR = (1<<7)
#define ENC28J60_SPI_MOSI_HIGH             ENC28J60_SPI_MOSI_GPIO_PORT->BSRR = (1<<7)
        
#define ENC28J60_CS_PIN                    GPIO_Pin_0                   /* PB.0 */
#define ENC28J60_CS_GPIO_PORT              GPIOB                        /* GPIOB */
#define ENC28J60_CS_GPIO_CLK               RCC_APB2Periph_GPIOB
#define ENC28J60_CS_LOW                    ENC28J60_CS_GPIO_PORT->BRR = (1<<0)
#define ENC28J60_CS_HIGH                   ENC28J60_CS_GPIO_PORT->BSRR = (1<<0)

#define ENC28J60_RST_PIN                    GPIO_Pin_1                   /* PB.1 */
#define ENC28J60_RST_GPIO_PORT              GPIOB                        /* GPIOB */
#define ENC28J60_RST_GPIO_CLK               RCC_APB2Periph_GPIOB
#define ENC28J60_RST_LOW                    ENC28J60_RST_GPIO_PORT->BRR = (1<<1)
#define ENC28J60_RST_HIGH                   ENC28J60_RST_GPIO_PORT->BSRR = (1<<1)

#define ENC28J60_INT_PIN                    GPIO_Pin_3                   /* PA.3 */
#define ENC28J60_INT_GPIO_PORT              GPIOA                        /* GPIOA */
#define ENC28J60_INT_GPIO_CLK               RCC_APB2Periph_GPIOA
#define ENC28J60_INT_INPUT                  (((ENC28J60_INT_GPIO_PORT->IDR)>>3)&0x01)

//--------------------------------------------------------------
// khai bao chan SPI2 ket noi voi FLASH
#define sFLASH_SPI                       SPI2
#define sFLASH_SPI_CLK                   RCC_APB1Periph_SPI2

#define sFLASH_SPI_SCK_PIN               GPIO_Pin_13                 /* PB.13 */
#define sFLASH_SPI_SCK_GPIO_PORT         GPIOB                       /* GPIOB */
#define sFLASH_SPI_SCK_GPIO_CLK          RCC_APB2Periph_GPIOB

#define sFLASH_SPI_MISO_PIN              GPIO_Pin_14                 /* PB.14 */
#define sFLASH_SPI_MISO_GPIO_PORT        GPIOB                       /* GPIOB */
#define sFLASH_SPI_MISO_GPIO_CLK         RCC_APB2Periph_GPIOB

#define sFLASH_SPI_MOSI_PIN              GPIO_Pin_15                 /* PB.15 */
#define sFLASH_SPI_MOSI_GPIO_PORT        GPIOB                       /* GPIOB */
#define sFLASH_SPI_MOSI_GPIO_CLK         RCC_APB2Periph_GPIOB

#define sFLASH_CS_PIN                    GPIO_Pin_8                 /* PA.8 */
#define sFLASH_CS_GPIO_PORT              GPIOA                       /* GPIOA */
#define sFLASH_CS_GPIO_CLK               RCC_APB2Periph_GPIOA
#define sFLASH_CS_LOW                    sFLASH_CS_GPIO_PORT->BRR = (1<<8)
#define sFLASH_CS_HIGH                   sFLASH_CS_GPIO_PORT->BSRR = (1<<8)


//--------------------------------------------------------------
// define Sensor
#define SS_CONVEYER_1_ACTIVE    0
#define SS_CARBIN_1_ACTIVE      0
#define SS_CONVEYER_2_ACTIVE    0
#define SS_CARBIN_2_ACTIVE      0
#define SS_CONVEYER_3_ACTIVE    0
#define SS_CARBIN_3_ACTIVE      0
#define SS_TOP_CARBIN_ACTIVE    0
#define SS_GATE_ACTIVE          0
#define SS_CARBIN_ACTIVE        0
#define SS_PRODUCT_OUT_ACTIVE   0

#if defined (__FW_BUILD__)
#define SS_CONVEYER_1_PIN       IN_SW0
#define SS_CARBIN_1_PIN         IN_SW1
#define SS_CONVEYER_2_PIN       IN_SW2
#define SS_CARBIN_2_PIN         IN_SW3
#define SS_CONVEYER_3_PIN       IN_SW4
#define SS_CARBIN_3_PIN         IN_SW5
#define SS_TOP_CARBIN_PIN       IN_SW6
#define SS_GATE_PIN             IN_SW7
#define SS_CARBIN_PIN           IN_SW8
#define SS_PRODUCT_OUT_PIN      IN_SW9
#else
#define SS_CONVEYER_1_PIN       1
#define SS_CARBIN_1_PIN         1
#define SS_CONVEYER_2_PIN       1
#define SS_CARBIN_2_PIN         1
#define SS_CONVEYER_3_PIN       1
#define SS_CARBIN_3_PIN         1
#define SS_TOP_CARBIN_PIN       1
#define SS_GATE_PIN             1
#define SS_CARBIN_PIN           1
#define SS_PRODUCT_OUT_PIN      1
#endif

#define IN_SW0              (((GPIOC->IDR)>>3)&0x01)
#define IN_SW1              (((GPIOB->IDR)>>5)&0x01)
#define IN_SW2              (((GPIOB->IDR)>>6)&0x01)
#define IN_SW3              (((GPIOB->IDR)>>7)&0x01)
#define IN_SW4              (((GPIOB->IDR)>>8)&0x01)
#define IN_SW5              (((GPIOC->IDR)>>10)&0x01)
#define IN_SW6              (((GPIOC->IDR)>>11)&0x01)
#define IN_SW7              (((GPIOC->IDR)>>12)&0x01)
#if defined MACHINE2
#define IN_SW8              (((GPIOC->IDR)>>4)&0x01)
#else
#define IN_SW8              (((GPIOD->IDR)>>2)&0x01)
#endif
#define IN_SW9              (((GPIOB->IDR)>>9)&0x01)

#define SW0_PERIPH          RCC_APB2Periph_GPIOC
#define SW0_PORT            GPIOC
#define SW0_PIN             GPIO_Pin_3

#define SW1_PERIPH          RCC_APB2Periph_GPIOB
#define SW1_PORT            GPIOB
#define SW1_PIN             GPIO_Pin_5

#define SW2_PERIPH          RCC_APB2Periph_GPIOB
#define SW2_PORT            GPIOB
#define SW2_PIN             GPIO_Pin_6

#define SW3_PERIPH          RCC_APB2Periph_GPIOB
#define SW3_PORT            GPIOB
#define SW3_PIN             GPIO_Pin_7

#define SW4_PERIPH          RCC_APB2Periph_GPIOB
#define SW4_PORT            GPIOB
#define SW4_PIN             GPIO_Pin_8

#define SW5_PERIPH          RCC_APB2Periph_GPIOC
#define SW5_PORT            GPIOC
#define SW5_PIN             GPIO_Pin_10

#define SW6_PERIPH          RCC_APB2Periph_GPIOC
#define SW6_PORT            GPIOC
#define SW6_PIN             GPIO_Pin_11

#define SW7_PERIPH          RCC_APB2Periph_GPIOC
#define SW7_PORT            GPIOC
#define SW7_PIN             GPIO_Pin_12

#if defined MACHINE2
#define SW8_PERIPH          RCC_APB2Periph_GPIOC
#define SW8_PORT            GPIOC
#define SW8_PIN             GPIO_Pin_4
#else
#define SW8_PERIPH          RCC_APB2Periph_GPIOD
#define SW8_PORT            GPIOD
#define SW8_PIN             GPIO_Pin_2
#endif

#define SW9_PERIPH          RCC_APB2Periph_GPIOB
#define SW9_PORT            GPIOB
#define SW9_PIN             GPIO_Pin_9

//--------------------------------------------------------------
// define USART for SIM900
#define RESET_PERIPH        RCC_APB2Periph_GPIOC
#define RESET_PORT          GPIOC
#define RESET_PIN           GPIO_Pin_13
#if defined (__FW_BUILD__)
#define SIM900_RESET        GPIOC->BRR = (1<<0)
#define SIM900_SET          GPIOC->BSRR  = (1<<0)
#else
#define SIM900_RESET        0
#define SIM900_SET          0
#endif

#define TX_RX_PERIPH        (RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO)
#define TX_RX_PORT          GPIOB
#define TX_PIN              GPIO_Pin_10
#define RX_PIN              GPIO_Pin_11

#define SIM900_USART_PERIPH         RCC_APB1Periph_USART3
#define SIM900_USART_ISR            USART3_IRQn
#define SIM900_USART_IRQHandler     USART3_IRQHandler
#define SIM900_USART                USART3

#endif


#endif  // __BoardHardware_h__

